const SFCScriptBlock = require('@vue/compiler-sfc')
const SFCDescriptor = require('@vue/compiler-sfc')

function relImportType(nodes) {
    const importMap = {}
    for (let item of nodes) {
        if (item.type === 'ImportDeclaration') {
            for (let specifier of item.specifiers) {
                importMap[specifier.local.name] = item.source.value
            }
        }
    }
    return importMap
}

/**
 * 解析引入
 * @param {SFCScriptBlock} scriptBlock vue解析后的Script块
 * @param {SFCDescriptor} descriptor vue格式化后的代码
 * @return {{[string]: string}|null}
 */
function relImports(scriptBlock, descriptor) {
    if (scriptBlock.scriptAst) {
        return relImportType(scriptBlock.scriptAst)
    } else if (scriptBlock.scriptSetupAst) {
        return relImportType(scriptBlock.scriptSetupAst)
    }
    return null
}

exports.relImports = relImports
